package mamikeji.liaowa.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import mamikeji.liaowa.R;
import mamikeji.liaowa.bean.Image;
import mamikeji.liaowa.bean.MomCircleBean;
import mamikeji.liaowa.ui.activity.circle.PreviewCircleImgActivity;
import mamikeji.liaowa.utils.ForContents;
import mamikeji.liaowa.utils.ForPreference;
import mamikeji.liaowa.widget.ninegrid.CustomImageView;
import mamikeji.liaowa.widget.ninegrid.NineGridlayout;
import mamikeji.liaowa.widget.ninegrid.ScreenTools;

/**
 * Created by 廉冲-android开发 on 2017/9/13.
 */
public class Mom_CircleAdapter extends RecyclerView.Adapter<Mom_CircleAdapter.ViewHolder> {
    List<MomCircleBean.DataEntity.Mam_circlesEntity> beanList = new ArrayList<>();
    private Context mContext;
    OnItemClickListener mOnItemClickListener;
    View view;
    boolean type_bool;

    public void addAllData(List<MomCircleBean.DataEntity.Mam_circlesEntity> beanList) {
        this.beanList.addAll(beanList);
        notifyDataSetChanged();
    }

    public void clearData() {
        this.beanList.clear();
    }

    public Mom_CircleAdapter(Context context , List<MomCircleBean.DataEntity.Mam_circlesEntity> beanList , boolean bool) {
        mContext = context;
        type_bool = bool;
    }

    public void setItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position);

        void onZanClick(int fc_id, int position);

        void onGuanZhuClick(String followusercode, int position);

        void onMSGClick(int fc_id, int position);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_mom_circle_list_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
        final MomCircleBean.DataEntity.Mam_circlesEntity dataEntity = beanList.get(position);

        String[] split2 = new String[]{};
        if (!dataEntity.getImgurl().equals("") && !dataEntity.getImgurl().equals("null") && dataEntity.getImgurl() != null) {
            split2 = dataEntity.getImgurl().split(",");
        }

        viewHolder.linearLayout_mom_circle_guanzhu.setVisibility(ForPreference.getString(
                ForContents.USER_CODE).equals(dataEntity.getUsercode()) ? View.GONE : View.VISIBLE);
        viewHolder.linearLayout_mom_circle_guanzhu.setBackgroundResource(dataEntity.getFollow() == 2 ?
                R.drawable.shape_momcircle_guanzhu_btn_bg_sel : R.color.color_ffffff);
        viewHolder.imageView_mom_circle_guanzhu.setVisibility(dataEntity.getFollow() == 2 ? View.VISIBLE : View.GONE);
        switch (dataEntity.getFollow()) {
            case 1:
                viewHolder.textView_mom_circle_guanzhu.setTextColor(mContext.getResources().getColor(R.color.color_text_333));
                viewHolder.textView_mom_circle_guanzhu.setText(R.string.momcircle_guanzhu_1);
                break;
            case 2:
                viewHolder.textView_mom_circle_guanzhu.setTextColor(mContext.getResources().getColor(R.color.color_ffffff));
                viewHolder.textView_mom_circle_guanzhu.setText(R.string.momcircle_guanzhu_2);
                break;
            case 3:
                viewHolder.textView_mom_circle_guanzhu.setTextColor(mContext.getResources().getColor(R.color.color_text_333));
                viewHolder.textView_mom_circle_guanzhu.setText(R.string.momcircle_guanzhu_3);
                break;
        }

        viewHolder.textView_mom_circle_type.setVisibility(type_bool ? View.VISIBLE : View.GONE);
        viewHolder.textView_mom_circle_nickname.setText(dataEntity.getName());
        if (dataEntity.getContent() == null || dataEntity.getContent().equals("")) {
            viewHolder.textView_mom_circle_content.setVisibility(View.GONE);
        } else {
            viewHolder.textView_mom_circle_content.setVisibility(View.VISIBLE);
            viewHolder.textView_mom_circle_content.setText(dataEntity.getContent());

            if (dataEntity.getContent().length() >= 20) {
                viewHolder.textView_mom_circle_content.setLines(2);
                viewHolder.textView_mom_circle_content.setEllipsize(TextUtils.TruncateAt.END);
            }
        }
        viewHolder.imageView_momcircle_item_identity.setImageResource(dataEntity.getCertification_name().endsWith("") ? R.drawable.icon_renzheng_n : R.drawable.icon_renzheng_h);

        String nameString = mContext.getResources().getString(R.string.momcircle_type);
        String nameFinal = String.format(nameString, dataEntity.getFc_typename());
        viewHolder.textView_mom_circle_type.setText(nameFinal);

        viewHolder.imageView_mom_circle_zannum.setImageResource(dataEntity.getPraisestate() == 1 ? R.drawable.icon_dianzan_h : R.drawable.icon_dianzan_n);
        viewHolder.textView_mom_circle_zannum.setText(dataEntity.getPraisenumber() + "");
        viewHolder.textView_mom_circle_recommend.setText(dataEntity.getViewnumber() + "");
        viewHolder.textView_mom_circle_time.setText(dataEntity.getBabyage());
        viewHolder.textView_mom_circle_creattime.setText(mistiming(dataEntity.getPublisdate()));

        viewHolder.imageView_mom_circle_titleimg.setImageURI(Uri.parse(dataEntity.getAvatar()));

        if (split2.length == 0) {
            viewHolder.ivMore.setVisibility(View.GONE);
            viewHolder.ivOne.setVisibility(View.GONE);
        } else if (split2.length == 1) {
            viewHolder.ivMore.setVisibility(View.GONE);
            viewHolder.ivOne.setVisibility(View.VISIBLE);

            handlerOneImage(viewHolder, new Image(split2[0], 250, 250));
        } else {
            viewHolder.ivMore.setVisibility(View.VISIBLE);
            viewHolder.ivOne.setVisibility(View.GONE);

            List<String> lsists = new ArrayList<>();
            for (int i = 0; i < split2.length; i++) {
                lsists.add(split2[i]);
            }

            viewHolder.ivMore.setImagesData(lsists);
        }

        final String[] finalSplit = split2;
        viewHolder.ivMore.setOnItemClickListener(new NineGridlayout.OnItemClickListener() {
            @Override
            public void onMultipleImgClick(int position) {
                ArrayList<String> stringList = new ArrayList<String>();
                for (int i = 0; i < finalSplit.length; i++) {
                    stringList.add(finalSplit[i]);
                }

                Intent intent = new Intent(mContext, PreviewCircleImgActivity.class);
                intent.putStringArrayListExtra("urlList", stringList);
                intent.putExtra("position", position);
                mContext.startActivity(intent);
            }
        });
        viewHolder.ivOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ArrayList<String> stringList = new ArrayList<String>();
                for (int i = 0; i < finalSplit.length; i++) {
                    stringList.add(finalSplit[i]);
                }

                Intent intent = new Intent(mContext, PreviewCircleImgActivity.class);
                intent.putStringArrayListExtra("urlList", stringList);
                intent.putExtra("position", 1);
                mContext.startActivity(intent);
            }
        });

        viewHolder.imageView_mom_circle_zannum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickListener.onZanClick(dataEntity.getFcid(), position);
            }
        });

        viewHolder.imageView_mom_circle_titleimg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickListener.onMSGClick(dataEntity.getFcid(), position);
            }
        });

        viewHolder.linearLayout_mom_circle_guanzhu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (dataEntity.getFollow() == 2) {
                    mOnItemClickListener.onGuanZhuClick(dataEntity.getUsercode(), position);
                }
            }
        });

        viewHolder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickListener.onItemClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return beanList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public View mView;

        public TextView textView_mom_circle_type;
        public TextView textView_mom_circle_creattime;
        public TextView textView_mom_circle_time;
        public TextView textView_mom_circle_nickname;
        public TextView textView_mom_circle_zannum;
        public TextView textView_mom_circle_recommend;
        public TextView textView_mom_circle_content;
        public SimpleDraweeView imageView_mom_circle_titleimg;
        public NineGridlayout ivMore;
        public CustomImageView ivOne;
        public ImageView imageView_mom_circle_zannum;
        public ImageView imageView_momcircle_item_identity;

        public TextView textView_mom_circle_guanzhu;
        public ImageView imageView_mom_circle_guanzhu;
        public LinearLayout linearLayout_mom_circle_guanzhu;

        public ViewHolder(View view) {
            super(view);
            mView = view;

            textView_mom_circle_type = (TextView) view.findViewById(R.id.textView_mom_circle_type);
            textView_mom_circle_nickname = (TextView) view.findViewById(R.id.textView_mom_circle_nickname);
            textView_mom_circle_zannum = (TextView) view.findViewById(R.id.textView_mom_circle_zannum);
            textView_mom_circle_recommend = (TextView) view.findViewById(R.id.textView_mom_circle_recommend);
            textView_mom_circle_content = (TextView) view.findViewById(R.id.textView_mom_circle_content);
            imageView_mom_circle_titleimg = (SimpleDraweeView) view.findViewById(R.id.imageView_mom_circle_titleimg);
            textView_mom_circle_time = (TextView) view.findViewById(R.id.textView_mom_circle_time);
            textView_mom_circle_creattime = (TextView) view.findViewById(R.id.textView_mom_circle_creattime);
            imageView_mom_circle_zannum = (ImageView) view.findViewById(R.id.imageView_mom_circle_zannum);
            imageView_momcircle_item_identity = (ImageView) view.findViewById(R.id.imageView_mom_circle_item_identity);
            imageView_mom_circle_guanzhu = (ImageView) view.findViewById(R.id.imageView_mom_circle_guanzhu);
            linearLayout_mom_circle_guanzhu = (LinearLayout) view.findViewById(R.id.linearLayout_mom_circle_guanzhu);
            textView_mom_circle_guanzhu = (TextView) view.findViewById(R.id.textView_mom_circle_guanzhu);

            ivMore = (NineGridlayout) view.findViewById(R.id.iv_ngrid_mom_circle);
            ivOne = (CustomImageView) view.findViewById(R.id.iv_oneimage_mom_circle);
        }
    }

    //判断时间差：新闻发布时间与当前系统时间的差值：如果小于60分钟换算成分钟；大于60分钟，换算成小时；大于24小时，换算成天
    public String mistiming(String time) {
        String date1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date());

        SimpleDateFormat CurrentTime = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

        Date beginTime = null;
        try {
            beginTime = CurrentTime.parse(date1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date endTime = null;
        try {
            endTime = CurrentTime.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //进行判断
        Long shijianCha = beginTime.getTime() - endTime.getTime();
        int count = 0;
        if (shijianCha > 24 * 60 * 60 * 1000) {
            count = (int) (shijianCha / (24 * 60 * 60 * 1000));
            return count + "天前";
        } else if (shijianCha > 60 * 60 * 1000) {
            count = (int) (shijianCha / (60 * 60 * 1000));
            return count + "小时前";
        } else if (shijianCha > 60 * 1000) {
            count = (int) (shijianCha / (60 * 1000));
            return count + "分钟前";
        } else {
            return "刚刚";
        }
    }

    private void handlerOneImage(ViewHolder viewHolder, Image image) {
        int totalWidth;
        int imageWidth;
        int imageHeight;
        ScreenTools screentools = ScreenTools.instance(mContext);
        totalWidth = screentools.getScreenWidth() - screentools.dip2px(80);
        imageWidth = screentools.dip2px(image.getWidth());
        imageHeight = screentools.dip2px(image.getHeight());
        if (image.getWidth() <= image.getHeight()) {
            if (imageHeight > totalWidth) {
                imageHeight = totalWidth;
                imageWidth = (imageHeight * image.getWidth()) / image.getHeight();
            }
        } else {
            if (imageWidth > totalWidth) {
                imageWidth = totalWidth;
                imageHeight = (imageWidth * image.getHeight()) / image.getWidth();
            }
        }
        ViewGroup.LayoutParams layoutparams = viewHolder.ivOne.getLayoutParams();
        layoutparams.height = imageHeight;
        layoutparams.width = imageWidth;
        viewHolder.ivOne.setLayoutParams(layoutparams);
        viewHolder.ivOne.setClickable(true);
        viewHolder.ivOne.setScaleType(ImageView.ScaleType.FIT_XY);
        viewHolder.ivOne.setImageUrl(image.getUrl());

    }
}
